package edu.sctu2;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class DateBase {
    public List<User> getDate(){
        List<User> list=new ArrayList<User>();

        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        Connection connection=null;
        Statement statement=null;
        ResultSet resultSet=null;

        try {
            connection= DriverManager.getConnection("jdbc:mysql://localhost:3306/sctu_javaweb_2018?serverTimezone=UTC&characterEncoding=gbk",
                    "root",
                    "w19971201t");
            statement=connection.createStatement();
            resultSet=statement.executeQuery("select * from t_user2");
            while (resultSet.next()){

                //将数据库的数据封装在集合中
                User user=new User();
                user.setId(resultSet.getInt(1));
                user.setUsername(resultSet.getString(2));
                user.setPassword(resultSet.getString(3));
                list.add(user);

            }



        } catch (SQLException e) {
            e.printStackTrace();
        }

finally {
            try {
                connection.close();
                statement.close();
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return list;
    }
}
